package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacInstrAntibDict;
import com.ysd.lis.mapper.bac.BacInstrAntibDictMapper;
import com.ysd.lis.request.bac.AddBacInstrAntibOrGermDictParam;
import com.ysd.lis.request.bac.DeleteBacInstrAntibOrGermDictParam;
import com.ysd.lis.service.bac.BacInstrAntibDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 细菌仪抗生素字典库 服务实现类
 * </p>
 *
 * @author wanghao
 * @create 2024/1/12 0012
 */
@Service
public class BacInstrAntibDictServiceImpl extends ServiceImpl<BacInstrAntibDictMapper, BacInstrAntibDict> implements BacInstrAntibDictService {

    @Autowired
    private BacInstrAntibDictMapper instrAntibDictMapper;
    @Autowired
    private BacInstrAntibDictService instrAntibDictService;

    @Override
    public Result deleteInstrAntibDict(DeleteBacInstrAntibOrGermDictParam deleteBacInstrAntibOrGermDictParam) {
        if (CollectionUtils.isEmpty(deleteBacInstrAntibOrGermDictParam.getIds())) {
            return Result.fail("请选择一条关联数据！");
        } else {
            boolean b = this.removeByIds(deleteBacInstrAntibOrGermDictParam.getIds());
            if (b) {
                return Result.succ(1, "删除成功！", null);
            } else {
                return Result.fail("删除失败！");
            }
        }
    }

    @Override
    public Result saveInstrAntibDict(List<BacInstrAntibDict> bacInstrAntibDicts) {
        instrAntibDictService.saveOrUpdateBatch(bacInstrAntibDicts);
        return Result.succ(1, "保存成功", null);
    }

    @Override
    public Result addInstrAntibDict(AddBacInstrAntibOrGermDictParam addBacInstrAntibDictParam) {
        //删除关联数据
        LambdaQueryWrapper<BacInstrAntibDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacInstrAntibDict::getDelFlag, 0);
        queryWrapper.eq(BacInstrAntibDict::getInstrCode, addBacInstrAntibDictParam.getInstrCode());
        queryWrapper.eq(BacInstrAntibDict::getAntCode, addBacInstrAntibDictParam.getAntibOrGermNo());
        instrAntibDictMapper.delete(queryWrapper);

        BacInstrAntibDict bacInstrAntibDict = new BacInstrAntibDict();
        bacInstrAntibDict.setAntCode(addBacInstrAntibDictParam.getAntibOrGermNo());
        BeanUtil.copyProperties(addBacInstrAntibDictParam, bacInstrAntibDict);
        boolean save = this.save(bacInstrAntibDict);
        if (save) {
            return Result.succ(1, "", null);
        } else {
            return Result.fail("添加失败！");
        }
    }
}
